package com.gem.talkbase.util;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.gem.talkbase.client.Client;
import com.gem.talkbase.util.StreamTools;





public class LoginService {
	public  static String  serchByGet(  String username ){
		//提交数据到服务器
		//拼装数据路径
		try{
			String path = Client.path_url+"?username="+username;
			URLEncoder.encode(path, "utf-8");
			URL url = new URL(path);
			System.out.println(username);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();

			conn.setConnectTimeout(5000);
			conn.setRequestMethod("GET");
			int code = conn.getResponseCode();
			System.out.println("发送get请求了吗。。。。");
			if(code==200){
				//请求成功 	
				System.out.println("get请求成功。。。。");
				InputStream is = conn.getInputStream();
				String text = StreamTools.readInputStream(is);
				return text;

			}else{
				return "获取数据失败!";
			}

		}catch(Exception e){
			e.printStackTrace();

		}

		return null;

	}
	
	
	
	
	
	

	public  static String  loginByPost(  String username ){
		//提交数据到服务器
		//拼装数据路径
		try{
			String path = Client.path_url;  //提交数据的路径
			URLEncoder.encode(path, "utf-8");         //utf-8编码
			URL url = new URL(path);       
			//System.out.println(username);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();

			conn.setConnectTimeout(5000);   //设置请求的超时时间
			conn.setRequestMethod("POST");  //post提交
			String data = "username="+URLEncoder.encode(username,"utf-8");//设置请求的编码格式，get最后的用处是计算提交数据的长度。
			conn.setRequestProperty("Content_Type", "application/x-www-from-urlencoded"); //设置文本信息
			conn.setRequestProperty("Content-Length", data.length()+"");//设置提交数据的长度

			//post 的方式 实际上是浏览器把数据写给了服务器
			conn.setDoOutput(true);              //设置输出的方式
			OutputStream os = conn.getOutputStream(); 
			os.write(data.getBytes());          //把数据写出去

			int code = conn.getResponseCode();  //设置请求的编码
			System.out.println("发送get请求了吗。。。。");
			if(code==200){        //code=200表示请求成功
				//请求成功 	
				System.out.println("get请求成功。。。。");
				InputStream is = conn.getInputStream();
				String text = StreamTools.readInputStream(is);
				System.out.println("text........"+text);
				return text;             //请求成功则返回的文本信息

			}else{
				return "获取数据失败!";
			}

		}catch(Exception e){
			e.printStackTrace();

		}

		return null;

	}



	public static String loginByClientGet(String username){
		try{

			HttpClient client = new DefaultHttpClient();
			String path = Client.path_url+"?username="
					+URLEncoder.encode(username);  //提交数据的路径
			HttpGet httpGet = new HttpGet(path);
			HttpResponse response = client.execute(httpGet);
			int code = response.getStatusLine().getStatusCode();

			if(code==200){        //code=200表示请求成功
				//请求成功 	
				System.out.println("get请求成功。。。。");
				InputStream is = response.getEntity().getContent();
				String text = StreamTools.readInputStream(is);
				System.out.println("text........"+text);
				return text;             //请求成功则返回的文本信息

			}else{
				return "获取数据失败!";
			}


		}catch(Exception e){
			e.printStackTrace(); 
			return null;
		}

	}


	public static String loginByClientPost(String username){
		try{

			HttpClient client = new DefaultHttpClient();
			String path = Client.path_url;//提交数据的路径
			HttpPost httpPost =new HttpPost(path);
			//指定要提交数据的实体
			List<NameValuePair> parameters = new ArrayList<NameValuePair>();
			parameters.add( new BasicNameValuePair("username", username));
			httpPost.setEntity(new UrlEncodedFormEntity(parameters,"utf-8"));
			//敲回车
			HttpResponse response = client.execute(httpPost);
			int code = response.getStatusLine().getStatusCode();

			if(code==200){        //code=200表示请求成功
				//请求成功 	
				System.out.println("get请求成功。。。。");
				InputStream is = response.getEntity().getContent();
				String text = StreamTools.readInputStream(is);
				System.out.println("text........"+text);
				return text;             //请求成功则返回的文本信息
			}
			else{
				return "获取数据失败!";
			}
			}catch(Exception e){
				e.printStackTrace(); 
				return null;

			}

		}
}
























